﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using AnyOffice.Components;
using log4net;
namespace AnyOffice.general.file_folder
{
    public partial class readdisk : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.Clear();
            string str = (Request.QueryString["filename"] != null) ? Request.QueryString["filename"].ToString() : "";
            str = str.Replace("../", "");
            string str2 = "/attachment/file_folder/" + HttpContext.Current.User.Identity.Name;
            FileInfo info = new FileInfo(Server.MapPath(str2 + str));
            if (info.Exists)
            {
                string str4 = ".doc.xls.ppt.docx.xlsx.pptx";
                if (str4.IndexOf(info.Extension.ToLower()) > -1)
                {
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info.Name, Encoding.UTF8));
                    string str5 = info.Extension.ToLower();
                    if (str5 != null)
                    {
                        if (!(str5 == ".doc"))
                        {
                            if (str5 == ".xls")
                            {
                                Response.ContentType = "application/vnd.ms-excel";
                            }
                            else if (str5 == ".ppt")
                            {
                                Response.ContentType = "application/vnd.ms-powerpoint";
                            }
                            else if (str5 == ".docx")
                            {
                                Response.ContentType = "application/docx";
                            }
                            else if (str5 == ".xlsx")
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            }
                            else if (str5 == ".pptx")
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                            }
                        }
                        else
                        {
                            Response.ContentType = "application/msword";
                        }
                    }
                    int bufferSize = 10240;
                    FileStream stream = new FileStream(info.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize);
                    try
                    {
                        byte[] buffer = new byte[bufferSize];
                        int count = 0;
                        int offset = 0;
                        while ((count = stream.Read(buffer, offset, buffer.Length)) > 0)
                        {
                            Response.OutputStream.Write(buffer, offset, count);
                        }
                    }
                    catch (Exception exception)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception);
                        }
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
            }
        }
    }
}
